﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using ISS.CaseStudy.DBUtility;
using ISS.CaseStudy.Entities;

namespace ISS.CaseStudy.DAL
{
    public static class UserInfoDLL
    {
        public static DataSet GetAllInfos()
        {
            string sql = "select * from UserInfo order by ModifyTime desc";
            DataSet dataSet = Database.SpExecuteDataSet(sql);
            return dataSet;
        }

        public static UserInfo GetInfo(string id)
        {
            string sql = "select * from UserInfo where [Id]=@id";
            SqlParameter[] paramCollection = { new SqlParameter("@id", id)};
            DataSet dataSet = Database.SpExecuteDataSet(sql, paramCollection);
            DataTable dataTable = dataSet.Tables[0];
            if (dataTable.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dataTable.Rows[0];

            UserInfo userInfo = new UserInfo();
            userInfo.Id = id;
            userInfo.Name = row["Name"].ToString();
            userInfo.Password = row["Password"].ToString();
            userInfo.NickName = row["NickName"].ToString();

            if (!string.IsNullOrEmpty(row["CreateTime"].ToString()))
            {
                userInfo.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
            }

            if (!string.IsNullOrEmpty(row["ModifyTime"].ToString()))
            {
                userInfo.ModifyTime = DateTime.Parse(row["ModifyTime"].ToString());
            }

            return userInfo;
        }

        public static bool Update(UserInfo userInfo)
        {
            bool b = false;
            string sql = "UPDATE UserInfo SET [Name]=@Name , [Password]=@Password, [NickName]=@NickName, [ModifyTime]=@ModifyTime where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Name", userInfo.Name), 
                                                 new SqlParameter("@Password", userInfo.Password), 
                                                 new SqlParameter("@NickName", userInfo.NickName),
                                                 new SqlParameter("@ModifyTime", userInfo.ModifyTime),
                                                 new SqlParameter("@Id", userInfo.Id)};
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b= true;
            }
            return b;
        }

        public static bool Insert(UserInfo userInfo)
        {
            bool b = false;
            string sql = "insert into UserInfo([Name],[Password],[NickName],[CreateTime],[ModifyTime]) values (@Name,@Password,@NickName,@CreateTime,@ModifyTime)";
            SqlParameter[] paramCollection = { new SqlParameter("@Name", userInfo.Name), 
                                                 new SqlParameter("@Password", userInfo.Password), 
                                                 new SqlParameter("@NickName", userInfo.NickName),
                                                 new SqlParameter("@CreateTime", userInfo.CreateTime),
                                                 new SqlParameter("@ModifyTime", userInfo.ModifyTime) };
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }

        public static bool Delete(string id)
        {
            bool b = false;
            string sql = "delete from UserInfo where ([Id]=@Id)";
            SqlParameter[] paramCollection = { new SqlParameter("@Id", id) };
            int i = Database.SpExecuteNonQuery(sql, paramCollection);
            if (i > 0)
            {
                b = true;
            }
            return b;
        }
    }
}
